package com.open.party.cloud.view.examine.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.ExamineClassMemberEntity;
import com.sinothk.android.utils.XUtils;
import com.sinothk.android.utils.inters.OnSuperListener;

import java.util.ArrayList;
import java.util.List;

public class ExamineClassMemberCheckListAdapter extends RecyclerView.Adapter<ExamineClassMemberCheckListAdapter.ViewHolder> {

    // 展示数据
    protected ArrayList<ExamineClassMemberEntity> mData;
    private Context mContext;
    private OnSuperListener<ExamineClassMemberEntity> onSuperListener;

    public ExamineClassMemberCheckListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<ExamineClassMemberEntity> onSuperListener) {
        this.onSuperListener = onSuperListener;
    }

    public void setData(List<ExamineClassMemberEntity> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<ExamineClassMemberEntity> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.examine_class_member_check_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final ExamineClassMemberEntity itemData = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getUserName()));

        holder.userPhoneTv.setText("电话：" + XUtils.string().getNotNullValue(itemData.getUserPhone()));
        holder.unitNameTv.setText("单位：" + XUtils.string().getNotNullValue(itemData.getUnitName()));
        holder.userTypeTv.setText("职务：" + XUtils.string().getNotNullValue(itemData.getUserType()));

        if ("M".equals(itemData.getSex())) {
            holder.sexTv.setText("性别：男");
        } else {
            holder.sexTv.setText("性别：女");
        }

        if (mData.size() - 1 == position) {
            holder.bottomLineTv.setVisibility(View.VISIBLE);
        } else {
            holder.bottomLineTv.setVisibility(View.GONE);
        }

        if (onSuperListener != null) {
            holder.backBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSuperListener.onClick(position, itemData, "backBtn");
                }
            });

            holder.passBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSuperListener.onClick(position, itemData, "passBtn");
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout rootView;
        TextView titleTv, sexTv, userPhoneTv, unitNameTv, userTypeTv, bottomLineTv,backBtn,passBtn;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
            sexTv = itemView.findViewById(R.id.sexTv);
            userPhoneTv = itemView.findViewById(R.id.userPhoneTv);
            unitNameTv = itemView.findViewById(R.id.unitNameTv);
            userTypeTv = itemView.findViewById(R.id.userTypeTv);

            backBtn = itemView.findViewById(R.id.backBtn);
            passBtn = itemView.findViewById(R.id.passBtn);

            bottomLineTv = itemView.findViewById(R.id.bottomLineTv);
        }
    }
}